package com.usian.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.usian.reggie.common.R;
import com.usian.reggie.entity.Category;
import com.usian.reggie.entity.Employee;
import com.usian.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 分类保存
     *
     * @param category
     * @param session
     * @return
     */
    @PostMapping()
    public R saveCategory(@RequestBody Category category, HttpSession session){
        Employee employee = (Employee)session.getAttribute("user");
        if(category.getId()==null){
            //新增
            if(employee!=null){
                category.setCreateUser(employee.getId());
                category.setUpdateUser(employee.getId());
            }
            categoryService.saveCategory(category);
        }else{
            //修改
            if(employee!=null){
                category.setUpdateUser(employee.getId());
            }
            categoryService.updateCategory(category);
        }
        return R.success("分类保存成功");
    }


    /**
     * 分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10")Integer pageSize){

        PageInfo<Category> pageInfo = categoryService.findByPage(page, pageSize);
        return R.success(pageInfo);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping()
    public R deleteCategory(Long id){
        categoryService.deleteCategory(id);
        return R.success("删除成功");
    }


    /**
     * 导出分类
     *
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        try {
            //1:读取excel模版到inputStream
            InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");

            Workbook workbook =new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Row row = sheet.getRow(0);

            //设置统一样式
            List<CellStyle> cellStyleList = new ArrayList<>();
            for (int i = 0; i <=4 ; i++) {
                //得到每一个单元格的样式
                CellStyle cellStyle = row.getCell(i).getCellStyle();
                //存储到集合中
                cellStyleList.add(cellStyle);
            }

            //查询要导出的分类数据
            List<Category> categoryList = categoryService.findAllCategory();

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            for (int i = 0; i < categoryList.size(); i++){
                Category category = categoryList.get(i);  //获取一个类别
                Row row1 = sheet.createRow(i + 1);

                //类别类型
                Cell cell = row1.createCell(0);
                cell.setCellStyle(cellStyleList.get(0));
                cell.setCellValue(category.getType());


                //类别名字
                cell = row1.createCell(1);
                cell.setCellStyle(cellStyleList.get(1));
                cell.setCellValue(category.getName());

                //排序
                cell = row1.createCell(2);
                cell.setCellStyle(cellStyleList.get(2));
                cell.setCellValue(category.getSort());


                //创建时间
                cell = row1.createCell(3);
                cell.setCellStyle(cellStyleList.get(3));
                cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));


                //修改时间
                cell = row1.createCell(4);
                cell.setCellStyle(cellStyleList.get(4));
                cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));

            }
            //设置响应头 用输出流的方式输出文件
            response.setHeader("content-disposition","attachment;filename=category.xlsx");
            workbook.write(response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据分类id查询分类列表
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R list(Integer type){
        List<Category> categoryList = categoryService.findCategoryByType(type);
        return R.success(categoryList);
    }
}
